package com.prp.gwt.client.user.php;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.vo.MessageList;
import com.prp.gwt.client.vo.SubjectRecommendList;
import com.prp.gwt.client.vo.UserList;
import com.prp.gwt.client.vo.UserStateList;
import com.prp.gwt.client.widgets.user.php.MsgBoardPanel;
import com.prp.gwt.client.widgets.user.php.RecentRecommendPanel;
import com.prp.gwt.client.widgets.user.php.UserInfoPanel;
import com.prp.gwt.client.widgets.user.home.MsgBoardRemindPanel;
import com.prp.gwt.client.widgets.user.home.PersonalArticalRecommend;
import com.prp.gwt.client.widgets.user.home.UserRecommendPanel;
import com.prp.gwt.client.widgets.user.php.UserStateListPanel;
import com.prp.gwt.client.widgets.user.home.UserStatePanel;

public class PHPListener {

	private static PHPListener instance;
	private PHPServiceAsync service = GWT.create(PHPService.class);
	private boolean alreadyLoadRecommendUsers = false;
	private boolean alreadyLoadRecommendArticles = false;
	private SubjectRecommendList recommendList;
	private UserStateList latestState;
	private UserStateList stateList;
	private MessageList messageList;

	public static PHPListener getInstance() {
		if (instance == null)
			instance = new PHPListener();
		return instance;
	}

	public void getLatestState(int id) {
		if (latestState == null || id != -1)
			refreshLatestState(id);
		else {
			UserStatePanel.getInstance().load(instance.latestState);
			UserInfoPanel.getInstance().load(instance.latestState);
		}
	}

	private void refreshLatestState(final int id) {
		service.getLatestState(id, new AsyncCallback<UserStateList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserStateList result) {
				if (id == -1) {
					instance.latestState = result;
					getLatestState(-1);
				} else {
					UserInfoPanel.getInstance().load(result);
				}
			}
		});
	}

	public void addState(String content) {
		service.addState(content, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				UserStatePanel.getInstance().init();
				refreshLatestState(-1);
				refreshStateList(-1);
			}
		});
	}

	public void refreshStateList(final int id) {
		service.getStateList(id, new AsyncCallback<UserStateList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserStateList result) {
				if (id == -1) {
					instance.stateList = result;
					getUserStateList(-1);
				} else {
					UserStateListPanel.getInstance().load(result);
				}
			}
		});
	}

	public void getUserStateList(int id) {
		if (instance.stateList == null || id != -1)
			refreshStateList(id);
		else
			UserStateListPanel.getInstance().load(instance.stateList);
	}

	public void getUserRecommendation() {
		if (alreadyLoadRecommendUsers)
			return;
		service.getUserRecommendation(new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				alreadyLoadRecommendUsers = true;
				UserRecommendPanel.getInstance().load(result);
			}
		});
	}

	public void addMessage(String content, int toUserId, final int inUserId) {
		service.addMessage(content, toUserId, inUserId,
				new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(Void result) {
						MsgBoardPanel.getInstance().refresh();
						refreshMessageList(inUserId);
					}
				});
	}

	public void getMessageList(int userId) {
		if (instance.messageList == null || userId != -1)
			refreshMessageList(userId);
		else
			MsgBoardPanel.getInstance().load(instance.messageList);
	}

	public void refreshMessageList(final int userId) {
		service.getMessageList(userId, new AsyncCallback<MessageList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MessageList result) {
				if(userId == -1)
					instance.messageList = result;
				MsgBoardPanel.getInstance().load(result);
			}
		});
	}

	public void deleteMessage(int id) {
		service.deleteMessage(id, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				MsgBoardPanel.getInstance().refresh();
				refreshMessageList(-1);
			}
		});
	}

	public void getUncheckMessage() {
		service.getUncheckedMessageList(new AsyncCallback<MessageList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MessageList result) {
				MsgBoardRemindPanel.getInstance().load(result);
			}
		});
	}

	public void setMessageChecked(int id) {
		service.setMessageChecked(id, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {

			}
		});
	}

	public void getUserArticalRecommend() {
		if (alreadyLoadRecommendArticles)
			return;
		service
				.getArticalRecommendation(new AsyncCallback<SubjectRecommendList>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(SubjectRecommendList result) {
						alreadyLoadRecommendArticles = true;
						PersonalArticalRecommend.getIntance().load(result);
					}
				});
	}

	public void getUserRecentRecommend(int userId) {
		if (instance.recommendList == null || userId != -1)
			refreshUserRecentRecommend(userId);
		else
			RecentRecommendPanel.getInstance().load(instance.recommendList);
	}

	public void refreshUserRecentRecommend(final int userId) {
		service.getRecentRecommendation(userId,
				new AsyncCallback<SubjectRecommendList>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(SubjectRecommendList result) {
						if (userId == -1) {
							instance.recommendList = result;
						}
						RecentRecommendPanel.getInstance().load(result);
					}
				});
	}

}
